package com.lcm.weam.service.pro.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcm.weam.dao.pro.MemberMapper;
import com.lcm.weam.dao.pro.ProjectMapper;
import com.lcm.weam.entity.pro.Project;
import com.lcm.weam.entity.req.ProjectMap;
import com.lcm.weam.entity.resp.PageResult;
import com.lcm.weam.entity.resp.ProfileResult;
import com.lcm.weam.entity.sys.FileList;
import com.lcm.weam.service.pro.ProjectService;
import com.lcm.weam.util.AliOSSUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Override
    @Transactional
    public String createPro(Project project) {
        return projectMapper.createPro(project);
    }

    @Override
    @Transactional
    public void editPro(Project project) {
        projectMapper.updateById(project);
    }

    @Override
    @Transactional
    public void deletePro(String proid) {
        projectMapper.deleteById(proid);
        memberMapper.deleteByProid(proid);
    }

    @Override
    public List<Project> selectAllPro(String uid) {
        return projectMapper.selectAllPro(uid);
    }

    @Override
    public IPage<Project> selectUserPro(String uid, int pageIndex, int pageSize) {
        return projectMapper.selectUserPro(new Page<Project>(pageIndex, pageSize), uid);
    }

    @Override
    public IPage<Project> selectUserProTS(String uid, int pageIndex, int pageSize, ProjectMap projectMap) {
        return projectMapper.selectUserProTS(new Page<Project>(pageIndex, pageSize), uid, projectMap);
    }

    @Override
    public Project intoAPro(String proid) {
        return projectMapper.selectById(proid);
    }

    @Override
    public boolean checkUser(String proid) {
        Project project = projectMapper.selectById(proid);
        Object principal = SecurityUtils.getSubject().getPrincipal();
        if (principal instanceof ProfileResult){
            ProfileResult user = (ProfileResult) principal;
            return Objects.equals(project.getUid(), user.getUid());
        }
        return false;
    }

    //每次使用intoAPro接口都会将uid,proid,visiDate存入一个表中
    //每天23.59分将今日访问记录归档到累计访问记录中
    @Async
    @Scheduled(cron = "* * * 1 * ?")
    @Override
    public void scheduledCheck() {
        //预留功能
    }

    @Override
    public void uploadFileAliOSS(String proid, MultipartFile file, String filename) throws IOException {
        AliOSSUtil ossUtil = new AliOSSUtil();
        String url = ossUtil.upload(file, "projectFile/" + proid, filename, true);
        System.out.println(url);
    }

    @Override
    public PageResult<FileList> listFiles(String proid, int page, int num) {
        AliOSSUtil ossUtils = new AliOSSUtil();
        Map<String, Object> map = ossUtils.listFiles("projectFile/" + proid, page, num);
        PageResult<FileList> pageResult = new PageResult<>();
        pageResult.setNums((Integer) map.get("nums"));
        pageResult.setTotal((Integer) map.get("total"));
        pageResult.setRows((List<FileList>) map.get("rows"));
        return pageResult;
    }
}
